using System;
using System.Collections.Generic;
using System.Text;
using CommunityServer;
using CommunityServer.Components;
using e2 = CommunityServer.MailRoom.Components;
using Telligent.MailGateway.Common.Components;

namespace CommunityServerStuff.CSModules
{
    /// <summary>
    /// Sends an e-mail to the members of the MembershipAdministration group whenever someone registers.
    /// </summary>
    class RegistrationNotificationModule : ICSModule
    {

        #region Methods 

        #region Init

        /// <summary>
        /// 
        /// </summary>
        /// <param name="csa"></param>
        /// <param name="node"></param>
        public void Init(CSApplication csa, System.Xml.XmlNode node)
        {
            csa.PostUserUpdate += new CSUserEventHandler(csa_PostUserUpdate);

        }

        #endregion

        #region csa_PostUserUpdate

        /// <summary>
        /// 
        /// </summary>
        /// <param name="user"></param>
        /// <param name="e"></param>
        void csa_PostUserUpdate(User user, CSEventArgs e)
        {
            //EventLogs.Warn("RegistrationNotificationModule Started.", "Registration", 301);
            if (e.State == ObjectState.Create)
            {

                // Do we have an email address?
                if (user.Email == null)
                    return;

                // Get the email template we're going to use
                EmailTemplate email = e2.Emails.GetTemplate("NewUserAdminNotify", user, true);
                if (email == null)
                    return;

                //EventLogs.Warn("Creating a user.", "Registration", 301);
                CSContext cntx = CSContext.Current;
                
                UserQuery uq = new UserQuery();
                Role r = Roles.GetRole("MembershipAdministrator");
                uq.RoleID = r.RoleID;
                UserSet users = Users.GetUsers(uq, false);

                if (users.HasResults)
                {

                    foreach (User u in users.Users)
                    {
                        //EventLogs.Warn(u.Email, "Registration", 301);
                        email.To.Add(u.Email);
                    }

                    List<EmailTextToken> tokens = new List<EmailTextToken>();
                    tokens.AddRange(e2.EmailTemplateProvider.GetCommonFields());
                    tokens.AddRange(e2.EmailTemplateProvider.GetUserFields(user));

                    //User's profile information hasn't been saved yet, so we can't do this...
                    //tokens.Add(new EmailTextToken("commonname", user.Profile.CommonName));
                    //tokens.Add(new EmailTextToken("webaddress", user.Profile.WebAddress));

                    if (cntx.IsWebRequest)
                        tokens.Add(new EmailTextToken("useraccepturl", Globals.HostPath(cntx.Context.Request.Url) + SiteUrls.Instance().ControlPanelUserEdit(user.UserID)));
                    else
                        tokens.Add(new EmailTextToken("useraccepturl", Globals.HostPath(new Uri(cntx.RawUrl)) + SiteUrls.Instance().ControlPanelUserEdit(user.UserID)));
                    e2.Emails.QueueMessage(email, tokens);
                }
            }
        }

        #endregion

        #endregion

    }
}
